# Plot leave-one-out results

library(haven)
library(ggplot2)

outdir = 'output/pub/'
df = read_stata(paste0(outdir, 'leave_one_out.dta'))

# convert dependent variables to factor
df$dv = factor(
    df$dv,
    levels = c('bills', 'laws', 'laws_tighten', 'laws_loosen'),
    labels = c('Bills', 'Laws', 'Laws Tightening', 'Laws Loosening')
)

# convert independent variables to factor
df$iv = factor(
    df$iv,
    levels = c('shoot', 'shootXrepleg', 'shootXdemleg', 'shootXsplleg', 'trend'),
    labels = c('Shooting', 'Rep X Shooting', 'Dem X Shooting', 'Split X Shooting', 'Trend Shooting')
)

# confidence interval
df$lb = df$b - 1.96 * df$se
df$ub = df$b + 1.96 * df$se

# full sample (no dropped state)
full = subset(
    df,
    subset = fips == 0
)

# results dropping one state at a time
lout = subset(
    df,
    subset = fips != 0
)

# order states by coefficient
lout$rnk = ave(
    lout$b, lout$dv, lout$iv, FUN = function(x) rank(x, ties.method = 'random')
)

# theme for all plots
plt.theme = theme(
    text = element_text(family='Helvetica', colour = 'black'),
    panel.grid.minor = element_blank(),
    panel.grid.major = element_blank(),
    panel.background = element_rect(fill = 'white', colour = 'black'),
    strip.background = element_rect(fill = 'white'),
    strip.text = element_text(face = 'bold', size = 10, colour='black'),
    axis.ticks = element_line(size = 0.5),
    axis.text.y = element_text(size = 8, colour = 'black'),
    legend.key = element_rect(fill = NA, colour = NA)
)

# function for graphing non-political coefficients
leave_out_graph = function(ivcat, dvcat, filepath) {

    lout.samp = subset(lout, subset = iv == ivcat & dv == dvcat)
    full.samp = subset(full, subset = iv == ivcat & dv == dvcat)

    plt = ggplot() + #+ facet_grid(dv ~ iv) +
        geom_hline(data=full.samp, aes(yintercept=b), linetype=1) +
        geom_hline(data=full.samp, aes(yintercept=lb), linetype=2) +
        geom_hline(data=full.samp, aes(yintercept=ub), linetype=2) +
        geom_segment(data=lout.samp, aes(x=rnk, xend=rnk, y=lb, yend=ub), alpha=0.5) +
        # geom_point(data=lout.samp, aes(x=rnk, y=b)) +
        geom_label(
            data=lout.samp,
            aes(x=rnk, y=b, label=usps),
            size=2.5,
            label.padding=unit(0.1, 'lines'),
            label.size=0,
            fontface='bold'
        ) +
        labs(x = "", y = "Coefficient Estimate") +
        scale_x_continuous(breaks=c(), expand=c(0, 1)) +
        # annotate('text', x=0, y=full.samp$b, label=round(full.samp$b, 2)) +
        scale_y_continuous(breaks = scales::pretty_breaks(8)) +
        #scale_y_continuous(
        #    breaks=c(0, 0.1, full.samp$lb, full.samp$b, full.samp$ub, 0.2, 0.3),
        #    labels=function(x) round(x, 2)
        #) +
        plt.theme
    ggsave(filepath, plt, width=9, height=4)
}

leave_out_graph('Shooting', 'Bills', paste0(outdir, 'fig/loo_bills.pdf'))
leave_out_graph('Shooting', 'Laws', paste0(outdir, 'fig/loo_laws.pdf'))
leave_out_graph('Trend Shooting', 'Bills', paste0(outdir, 'fig/loo_trend_bills.pdf'))
leave_out_graph('Trend Shooting', 'Laws', paste0(outdir, 'fig/loo_trend_laws.pdf'))

leave_out_graph(
    'Rep X Shooting',
    'Laws Loosening',
    paste0(outdir, 'fig/loo_rep_loosen.pdf')
)

leave_out_graph(
    'Rep X Shooting',
    'Laws Tightening',
    paste0(outdir, 'fig/loo_rep_tighten.pdf')
)

leave_out_graph(
    'Dem X Shooting',
    'Laws Loosening',
    paste0(outdir, 'fig/loo_dem_loosen.pdf')
)

leave_out_graph(
    'Dem X Shooting',
    'Laws Tightening',
    paste0(outdir, 'fig/loo_dem_tighten.pdf')
)
